{% extends "./inc/base.html" %}
{% block content %}
<section>
    <div class="container">
        <div class="row bg1x1">
            <!-- LEFT -->
            {% include "./inc/user_left.html" %}
            <!-- RIGHT -->
            <div class="col-lg-10 col-md-10 col-sm-8">
                <h4 class="page-header margin-top-0"><i class="fa fa-reorder fa-lg text-default"></i>
                    {{controller.meta_title}}</h4>
                {# 会员表单 #}
                <div class="row">
                    <form id="formMembership" class="form-horizontal">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">选择会员类型</label>
                            <div class="col-sm-10">
                                <select id="memberGroupSelect" class="form-control" name="groupId" placeholder="请选择">
                                    <option value="">请选择</option>
                                    {% for item in groupsInfo %}
                                    {% if item.price_l !== 0 or item.price_y !== 0 or item.price_m !==0 or item.price_d !==0 %}
                                    <option value="{{ item.groupid }}">{{ item.name }}</option>
                                    {% endif %}
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">价格</label>
                            <div class="col-sm-10">
                                <span class="text-danger form-control" id="showPrice">￥0.00</span>
                                <input style="display: none;" id="buyPrice" name="price" value=0 />
                            </div>
                        </div>
                        <button type="submit" class="btn btn-default col-md-4 col-md-offset-4">提交</button>
                    </form>
                </div>
                <h4 class="page-header margin-top-10"><i class="fa fa-reorder fa-lg text-default"></i>
                    会员权限</h4>
                <div class="table-responsive">
                    {%set groupid = config.setup.SYS_PRIV_GROUP%}
                    <table class="table table-bordered table-vertical-middle">
                        <thead>
                            <tr>
                                <th>角色名称</th>
                                {% for index, items in groupid %}
                                <th>{{items}}</th>
                                {% endfor%}
                            </tr>
                        </thead>
                        <tbody>
                            {% for val in groupsInfo %}
                            <tr>
                                <td>
                                    <span style="color:{{val.usernamecolor}}">{{val.name}}</span>
                                </td>
                                {% for index, items in groupid %}
                                <td>
                                    <i
                                        class="{% if privGroupAuth[index]==undefined or index|in_Array(privGroup[val.groupid]) %}fa fa-check text-success{% else %}fa fa-times text-danger{%endif%}"></i>
                                </td>
                                {% endfor %}
                            </tr>
                            {%endfor%}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</section>
{% endblock %}
{% block script %}
<script>
    var memberGroupSelect = $('#memberGroupSelect');
    var timeTypeSelect = $('#timeTypeSelect');
    var timeSelect = $('#timeSelect');
    var showPrice = $('#showPrice');
    var buyPrice = $('#buyPrice');
    memberGroupSelect.on('change', function () {
        getPrice();
    });
    function getPrice() {
        showPrice.html('正在计算价格...');
        $.ajax({
            url: '/center/account/upgrade?step=1',
            method: 'get',
            data: $('#formMembership').serialize(),
            dataType: 'json',
            success: function (res) {
                if (res.errno) {
                    _toastr(res.errmsg, 'top-right', 'error', false);
                    showPrice.html('￥0.00元');
                    return;
                }
                var data = res.data;
                showPrice.html('当前会员价值 ￥' + data.old + '元, 选择的会员价值 ￥' + data.new + '元, 需要补差价 ￥' + data.upgrade + '元');
                buyPrice.val(data.upgrade);
            },
            error: function (e) {
                _toastr('计算价格发生错误', 'top-right', 'error', false);
            }
        });
    }
    $('#formMembership').submit(function () {
        $.ajax({
            url: '/center/account/upgrade?step=2',
            method: 'get',
            data: $('#formMembership').serialize(),
            dataType: 'json',
            success: function (res) {
                if (res.errno) {
                    _toastr(res.errmsg, 'top-right', 'error', false);
                    return;
                }
                _toastr(res.data, 'top-right', 'success', false);
                setTimeout(function () {
                    window.location = '/center/index';
                }, 3000);
            },
            error: function (e) {
                _toastr('计算价格发生错误', 'top-right', 'error', false);
            }
        });
        return false;
    });
</script>
{% endblock %}